package com.quan.web.controller.weixin;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.common.annotation.RequiresPermissions;
import com.quan.common.base.pojo.JSONResult;
import com.quan.common.base.pojo.PageBean;
import com.quan.common.core.support.BaseController;
import com.quan.weixin.rpc.api.WeixinArticleService;
import com.quan.weixin.rpc.model.WeixinArticle;

@RestController
@RequestMapping(value = "/api/weixin/article")
public class WeixinArticleController extends BaseController {

	@Autowired
	private WeixinArticleService weixinArticleService;

	@RequiresPermissions(value="weixin:article:add", description="添加微信图文消息")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody WeixinArticle entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return JSONResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.weixinArticleService.insertArticleContentWrapper(entity) ;

		return JSONResult.ok();
	}

	@RequiresPermissions(value="weixin:article:delete", description="删除微信图文消息")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.weixinArticleService.deleteById(id);
		return JSONResult.ok();
	}
	
	@RequiresPermissions(value="weixin:article:delete", description="删除微信文本消息")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.weixinArticleService.deleteByIds(ids) ;
		return JSONResult.ok();
	}

	@RequiresPermissions(value="weixin:article:update", description="修改微信图文消息")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody WeixinArticle entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return JSONResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.weixinArticleService.updateArticleContentWrapper(entity);

		return JSONResult.ok();
	}

	@RequiresPermissions(value="weixin:article:detail", description="查询微信图文消息详情")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.weixinArticleService.getArticleWrapper(id);
	}
	
	@RequiresPermissions(value="weixin:article:list", description="查询微信图文消息")
	@GetMapping(value = "/list")
	public Object list(WeixinArticle entity) {
		return this.weixinArticleService.selectPage(entity, new PageBean(request)) ;
	}
}
